import axios from "axios";


export default {
	// http://127.0.0.1:8081/ele/restapi/bgs/poi/search_poi_nearby_alipay?keyword=%E5%8C%97%E4%BA%AC&offset=0&limit=20&latitude=39.93482727239599&longitude=116.4224009776628
	//根据输入关键词 和 当前经纬的 搜索附件位置
	  search_poi_nearby_alipay(store,playload){
	return	axios.get("/ele/restapi/bgs/poi/search_poi_nearby_alipay",{params:{
			keyword:playload.keywords,
			offset:0,
			limit:20,
			latitude:playload.position.lat,
			longitude:playload.position.lng,
		}})
	},
	// 登录
	Login(store,playload){
		var data = {Mobile:playload.phone,Scf:"ms",validate_code:playload.msgCode,validate_token:playload.validate_token}
		axios.post("/ele/restapi/eus/login/login_by_mobile",data)
		.then(res=>{
		    console.log(' 登陆后res=>',res);
			if(res.status == 200){
				store.state.isLogin = true
				store.state.A = res.data.user_id
				
				sessionStorage.setItem("user_id",res.data.user_id)
				sessionStorage.setItem("isLogin",true)
				
			}
		})
	},
	   // 请求方法 把地址名称转化成对应的经纬度
	   getCoords(store, value) {
		axios.get("/geo/geocoder/v2/", {
			params: {
				address: value,
				ak: "p32oLMwF41j3GoTB6Gjh2vo8wthwGgil",
				output: "json"
			}
		}).then(res => {
	   		// 如果请求成功 赋值状态position
			console.log("getCoords转换成功!");
			var location = res.data.result.location
	   		store.commit("setPosition",{location:location,locationName:value})
			
			store.dispatch("getResturantList",{position:location,times:0})
					.then(res =>{
						store.state.restaurants = []
						store.state.restaurants.push(res.data.items)
						console.log(res.data.items);
					})
	   	}).catch(err => {
			console.log("getCoords转换失败!");
	   		console.log(err)
	   	})
	   },
	   //获取店铺列表
	   getResturantList(store,playload){

		    var lat = playload.position.lat==""?34.72893:playload.position.lat 
		   var lng =  playload.position.lng==""?113.752807:playload.position.lng 
		   // var lat = playload.position.lat
		   // var lng =  playload.position.lng
		   var limit = 10
		   var offset  = playload.times*limit
		   store.commit("setRestaurantReqTimes")
		   // axios.get(`/ele/restapi/shopping/v3/restaurants?latitude=${lat}&longitude=${lng}&offset=0&limit=100&extras[]=activities&extras[]=tags&extra_filters=home&rank_id=&terminal=h5`)
		  return  axios.get(`/ele/restapi/shopping/v3/restaurants?latitude=${lat}&longitude=${lng}&offset=${offset}&limit=${limit}&extras[]=activities&extras[]=tags&extra_filters=home&rank_id=&terminal=h5`)
	   },

	   // 返回当前经纬度对应的位置信息
	   getLocationInfoByLngLat(store,coords){
		   // https://h5.ele.me/restapi/bgs/poi/reverse_geo_coding?latitude=34.72893&longitude=113.752807
		  return  axios.get(`/ele/restapi/bgs/poi/reverse_geo_coding?latitude=${coords.lat}&longitude=${coords.lng}`)
	   },
	   //封装一个方法 当我们更新 仓库中的经纬度的时候 同时更新当前 位置名称
	   saveCurrentLocalInfo(store ,coords ){
		   axios.get(`/ele/restapi/bgs/poi/reverse_geo_coding?latitude=${coords.lat}&longitude=${coords.lng}`)
				.then(res =>{
					store.state.locationName= res.data.address
					store.state.position ={lat: pos.coords.latitude, lng:pos.coords.longitude} 
				})
	   },
	   //商戶的評論列表
	   getResEvaluate(store,resId){
		 return   axios.get(`/ele/pizza/ugc/restaurants/${resId}/batch_comments?has_content=true&offset=0&limit=20`)
	   }

}
